package com.engine.thread;

import java.util.List;
import java.util.concurrent.Callable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.api.taobao.ItemGet;
import com.biz.jdbc.Product;
import com.biz.jdbc.ProductDao;
import com.biz.result.ItemGetResult;
import com.taobao.api.domain.Item;
import com.taobao.api.domain.ItemImg;
import com.taobao.api.response.ItemGetResponse;
import com.util.ZLibUtils;

/**
 * 调用淘宝get API
 * @author liuchao
 * @version $Id: GetAPIThread.java,v 0.1 2012-9-23 上午9:35:26 liuchao Exp $
 */
public class GetAPICallable implements Callable<ItemGetResult> {
    
    protected   Log                             log               =           LogFactory.getLog(this.getClass());
    
    private Long iid;
    
    public GetAPICallable(Long iid){
        this.iid = iid;
    }

    @Override
    public ItemGetResult call() throws Exception {
        ProductDao productDao = new ProductDao();
        log.info("------------------------GetAPICallable "+iid+"------------------------") ;
        ItemGetResult result = new ItemGetResult();
        try{
            ItemGetResponse itemGetRes = ItemGet.getItemGetRes(iid);
            if( null != itemGetRes && itemGetRes.getErrorCode() != null){
                result.setErrorNOInfo(  Integer.parseInt( itemGetRes.getErrorCode()), itemGetRes.getSubMsg());
            }
            if( result.getErrorInfo().equals("该商品已被删除") && result.getErrorNO() == 530 ){
                //删除 该商品
                productDao.deleteByIid(iid);
            }
            Item goodsItem = itemGetRes.getItem();
            Product product = new Product();
            product.setProductId( goodsItem.getNumIid() );
            product.setCatId( goodsItem.getCid().intValue() );
            product.setPrice( Double.parseDouble( goodsItem.getPrice()));
            product.setPicUrl( goodsItem.getPicUrl() );
            product.setTitle( goodsItem.getTitle() );
            String testStr = goodsItem.getDesc();
            byte[] descData = ZLibUtils.compress(   testStr.getBytes()  );  
            product.setDesction(descData);
            product.setNickName( goodsItem.getNick() );
            product.setDetailUrl( goodsItem.getDetailUrl() );
            product.setPropsName( ZLibUtils.compress( goodsItem.getPropsName().getBytes()) );
            product.setProps( ZLibUtils.compress( goodsItem.getProps().getBytes()) );
            product.setGlobalStockType( "0" );
            if(goodsItem.getNum() == null){
                product.setNum(     0   );
            }else{
                product.setNum( goodsItem.getNum().intValue()   );
            }
            String imgUrls = "";
            List<ItemImg> itemImgUrls = goodsItem.getItemImgs();
            if( null != itemImgUrls && itemImgUrls.size() >0){
                for(int j=0;j<itemImgUrls.size();j++){
                    ItemImg itemimg = itemImgUrls.get(j);
                    if( j< itemImgUrls.size() && j!= itemImgUrls.size()-1){
                        imgUrls+= itemimg.getUrl()+",";
                    }
                    else{
                        imgUrls+= itemimg.getUrl();
                    }
                }
            }
            if( imgUrls.length()>0 ){
                product.setItemImgUrl(  ZLibUtils.compress( imgUrls.getBytes()));
            }else{
                product.setItemImgUrl( new String("").getBytes()    );
            }
            //修改UPDATE
            productDao.updateByIid(product);
        } catch(Exception ex){
            result.setErrorNOInfo(0001, "taobao api错误");
        }
        return result;
    }

}
